import { is, getBusinessObject } from 'bpmn-js/lib/util/ModelUtil'
import { without } from 'min-dash'
import { createModdleElement, getExtensionElementsList } from '@/utils/BpmnExtensionElementsUtil'
import editor from '@/store/editor'
import modeler from '@/store/modeler'

/////// 功能函数
export function getProperties(element) {
    const prefix = editor().getProcessEngine
    const businessObject = getBusinessObject(element);
    const formKey =
        businessObject.get(`${prefix}:formKeyLabel`) ||
        businessObject.get(`${prefix}:formKey`);
    const formRef = businessObject.get(`${prefix}:formRef`);
    const formRefBinding = businessObject.get(`${prefix}:formRefBinding`);
    const formRefVersion = businessObject.get(`${prefix}:dueDate`);
    let formHandlerClass;
    if (formKey !== undefined) {
        formHandlerClass = "formKey";
    } else if (formRef !== undefined) {
        formHandlerClass = "formRef";
    } else if (formRefBinding !== undefined || formRefVersion !== undefined) {
        formHandlerClass = "formData";
    }
    return {
        formKey: formKey,
        formRef: formRef,
        formRefBinding: formRefBinding,
        formRefVersion: formRefVersion,
        formHandlerClass: formHandlerClass
    };
}

// 增加FormData节点
export function addFormData(element) {    
    try {
        const modeling = modeler().getModeling
        const prefix = editor().getProcessEngine
        const businessObject = getRelevantBusinessObject(element);
        // 判断 extensionElements
        let extensionElements = businessObject.get("extensionElements");
        if (!extensionElements) {
            extensionElements = createModdleElement(
                "bpmn:ExtensionElements",
                { values: [] },
                businessObject
            );
            modeling?.updateModdleProperties(element, businessObject, {
                extensionElements
            });
        }

        // 创建新属性并添加
        const newFormData = createModdleElement(
            `${prefix}:FormData`,
            { fields: [] },
            extensionElements
        );
        modeling?.updateModdleProperties(element, extensionElements, {
            values: [...extensionElements.get("values"), newFormData]
        });
    } catch (e) {
        console.log(e);
    }
}

// 删除FormData节点
export function removeFormData(element) {
    const businessObject = getRelevantBusinessObject(element);
    const extensionElements = businessObject.get("extensionElements");
    const formData = getFormData(businessObject);
    if (!formData) return;
    const modeling = modeler().getModeling
    modeling?.updateModdleProperties(element, extensionElements, {
        values: without(extensionElements.get("values"), formData)
    });
}

///// helpers
function getRelevantBusinessObject(element) {
    const businessObject = getBusinessObject(element);
    if (is(element, "bpmn:Participant")) {
        return businessObject.get("processRef");
    }
    return businessObject;
}

// function getFormDataList(bo) {
//   const properties = getFormData(bo);
//   return properties && properties.get("values");
// }

function getFormData(bo) {
    const extensionElementsList = getExtensionElementsList(bo);
    const prefix = editor().getProcessEngine
    return extensionElementsList.filter(
        ex => ex.$type === `${prefix}:FormData`
    )?.[0];
}
